//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using Tathata.Common.Caching;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Globalization;
using Tathata.Common.DAL;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.EntityCollections;

namespace Tathata.Common.DAL.Managers
{
    internal class CategoryDBManager
    {
        #region Methods
        public static CategoryCollection GetAllCategories(int ParentCategoryID, bool showHidden)
        {
            CategoryCollection categoryCollection = new CategoryCollection();
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_CategoryLoadAll");
            db.AddInParameter(dbCommand, "ShowHidden", DbType.Boolean, showHidden);
            db.AddInParameter(dbCommand, "ParentCategoryID", DbType.Int32, ParentCategoryID);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    Category category = new Category();
                    category.CategoryID = NopDataHelper.GetInt(dataReader, "CategoryID");
                    category.Name = NopDataHelper.GetString(dataReader, "Name");
                    category.Description = NopDataHelper.GetString(dataReader, "Description");
                    category.TemplateID = NopDataHelper.GetInt(dataReader, "TemplateID");
                    category.MetaKeywords = NopDataHelper.GetString(dataReader, "MetaKeywords");
                    category.MetaDescription = NopDataHelper.GetString(dataReader, "MetaDescription");
                    category.MetaTitle = NopDataHelper.GetString(dataReader, "MetaTitle");
                    category.SEName = NopDataHelper.GetString(dataReader, "SEName");
                    category.ParentCategoryID = NopDataHelper.GetInt(dataReader, "ParentCategoryID");
                    category.Published = NopDataHelper.GetBoolean(dataReader, "Published");
                    category.PictureID = NopDataHelper.GetInt(dataReader, "PictureID");
                    category.Deleted = NopDataHelper.GetBoolean(dataReader, "Deleted");
                    category.DisplayOrder = NopDataHelper.GetInt(dataReader, "DisplayOrder");
                    category.CreatedOn = NopDataHelper.GetDateTime(dataReader, "CreatedOn");
                    category.UpdatedOn = NopDataHelper.GetDateTime(dataReader, "UpdatedOn");
                    categoryCollection.Add(category);
                }
            }

            return categoryCollection;
        }

        public static Category GetByCategoryID(int CategoryID)
        {

            Category category = null;
            if (CategoryID == 0)
                return category;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_CategoryLoadByPrimaryKey");
            db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, CategoryID);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    category = new Category();
                    category.CategoryID = NopDataHelper.GetInt(dataReader, "CategoryID");
                    category.Name = NopDataHelper.GetString(dataReader, "Name");
                    category.Description = NopDataHelper.GetString(dataReader, "Description");
                    category.TemplateID = NopDataHelper.GetInt(dataReader, "TemplateID");
                    category.MetaKeywords = NopDataHelper.GetString(dataReader, "MetaKeywords");
                    category.MetaDescription = NopDataHelper.GetString(dataReader, "MetaDescription");
                    category.MetaTitle = NopDataHelper.GetString(dataReader, "MetaTitle");
                    category.SEName = NopDataHelper.GetString(dataReader, "SEName");
                    category.ParentCategoryID = NopDataHelper.GetInt(dataReader, "ParentCategoryID");
                    category.PictureID = NopDataHelper.GetInt(dataReader, "PictureID");
                    category.Published = NopDataHelper.GetBoolean(dataReader, "Published");
                    category.Deleted = NopDataHelper.GetBoolean(dataReader, "Deleted");
                    category.DisplayOrder = NopDataHelper.GetInt(dataReader, "DisplayOrder");
                    category.CreatedOn = NopDataHelper.GetDateTime(dataReader, "CreatedOn");
                    category.UpdatedOn = NopDataHelper.GetDateTime(dataReader, "UpdatedOn");
                }
            }
            return category;
        }

        public static Category InsertCategory(string Name, string Description,
            int TemplateID, string MetaKeywords, string MetaDescription, string MetaTitle,
            string SEName, int ParentCategoryID, int PictureID, bool Published, bool Deleted,
            int DisplayOrder, DateTime CreatedOn, DateTime UpdatedOn)
        {
            Category category = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_CategoryInsert");
            db.AddOutParameter(dbCommand, "CategoryID", DbType.Int32, 0);
            db.AddInParameter(dbCommand, "Name", DbType.String, Name);
            db.AddInParameter(dbCommand, "Description", DbType.String, Description);
            db.AddInParameter(dbCommand, "TemplateID", DbType.Int32, TemplateID);
            db.AddInParameter(dbCommand, "MetaKeywords", DbType.String, MetaKeywords);
            db.AddInParameter(dbCommand, "MetaDescription", DbType.String, MetaDescription);
            db.AddInParameter(dbCommand, "MetaTitle", DbType.String, MetaTitle);
            db.AddInParameter(dbCommand, "SEName", DbType.String, SEName);
            db.AddInParameter(dbCommand, "ParentCategoryID", DbType.Int32, ParentCategoryID);
            db.AddInParameter(dbCommand, "PictureID", DbType.Int32, PictureID);
            db.AddInParameter(dbCommand, "Published", DbType.Boolean, Published);
            db.AddInParameter(dbCommand, "Deleted", DbType.Boolean, Deleted);
            db.AddInParameter(dbCommand, "DisplayOrder", DbType.Int32, DisplayOrder);
            db.AddInParameter(dbCommand, "CreatedOn", DbType.DateTime, CreatedOn);
            db.AddInParameter(dbCommand, "UpdatedOn", DbType.DateTime, UpdatedOn);
            if (db.ExecuteNonQuery(dbCommand) > 0)
            {
                int CategoryID = Convert.ToInt32(db.GetParameterValue(dbCommand, "@CategoryID"));
                category = GetByCategoryID(CategoryID);
            }
            return category;
        }

        public static Category UpdateCategory(int CategoryID, string Name, string Description,
            int TemplateID, string MetaKeywords, string MetaDescription, string MetaTitle,
            string SEName, int ParentCategoryID, int PictureID, bool Published, bool Deleted,
            int DisplayOrder, DateTime CreatedOn, DateTime UpdatedOn)
        {
            Category category = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_CategoryUpdate");
            db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, CategoryID);
            db.AddInParameter(dbCommand, "Name", DbType.String, Name);
            db.AddInParameter(dbCommand, "Description", DbType.String, Description);
            db.AddInParameter(dbCommand, "TemplateID", DbType.Int32, TemplateID);
            db.AddInParameter(dbCommand, "MetaKeywords", DbType.String, MetaKeywords);
            db.AddInParameter(dbCommand, "MetaDescription", DbType.String, MetaDescription);
            db.AddInParameter(dbCommand, "MetaTitle", DbType.String, MetaTitle);
            db.AddInParameter(dbCommand, "SEName", DbType.String, SEName);
            db.AddInParameter(dbCommand, "ParentCategoryID", DbType.Int32, ParentCategoryID);
            db.AddInParameter(dbCommand, "PictureID", DbType.Int32, PictureID);
            db.AddInParameter(dbCommand, "Published", DbType.Boolean, Published);
            db.AddInParameter(dbCommand, "Deleted", DbType.Boolean, Deleted);
            db.AddInParameter(dbCommand, "DisplayOrder", DbType.Int32, DisplayOrder);
            db.AddInParameter(dbCommand, "CreatedOn", DbType.DateTime, CreatedOn);
            db.AddInParameter(dbCommand, "UpdatedOn", DbType.DateTime, UpdatedOn);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                category = GetByCategoryID(CategoryID);

            return category;
        }

        #endregion
    }
}
